import time
from selenium import webdriver
from selenium.webdriver.support.select import Select


class vip_user:
    def __init__(self):
        host = "http://192.168.0.118"
        wd = webdriver.Firefox()
        wd.maximize_window()   #浏览器窗口全屏
        wd.get(host+"/TinyShop//index.php?con=admin&act=login")
        self.wd = wd
    def login(self,user,pwd):
        wd = self.wd
        wd.find_element_by_name("name").clear()
        wd.find_element_by_name("name").click()
        wd.find_element_by_name("name").send_keys(user)
        wd.find_element_by_name("password").clear()
        wd.find_element_by_name("password").click()
        wd.find_element_by_name("password").send_keys(pwd)
        wd.find_element_by_name("verifyCode").clear()
        wd.find_element_by_name("verifyCode").click()
        wd.find_element_by_name("verifyCode").send_keys("aaaa")
        wd.find_element_by_xpath("//input[@value=\"登 录\"]").click()
    def vipuser_add(self,name,pwd,repwd,email,re_name,birthday,sheng,ss,qu,addr,phone,mobile,point=0):
        time.sleep(0.5)
        wd = self.wd
        wd.find_element_by_link_text("客户中心").click()
        wd.find_element_by_link_text("添加").click()
        wd.find_element_by_name("name").click()
        wd.find_element_by_name("name").send_keys(name)
        wd.find_element_by_name("password").click()
        wd.find_element_by_name("password").send_keys(pwd)
        wd.find_element_by_name("repassword").click()
        wd.find_element_by_name("repassword").send_keys(repwd)
        wd.find_element_by_name("email").click()
        wd.find_element_by_name("email").send_keys(email)
        wd.find_element_by_name("real_name").click()
        wd.find_element_by_name("real_name").send_keys(re_name)
        wd.find_element_by_xpath("//input[@checked=\"checked\"]").click()
        day = ''
        if birthday != "":
            wd.find_element_by_name("birthday").click()
            wd.find_element_by_name("birthday").send_keys(birthday)
            wd.find_element_by_xpath("//input[@checked=\"checked\"]").click()
            try:
                elel=wd.switch_to.alert   #切换到警告框
                time.sleep(0.5)
                elel.accept()
                day = "不合法的日期格式或者日期超出限定范围！此项不是必填项已置为空!"
            except:
                wd.find_element_by_xpath("//input[@checked=\"checked\"]").click()

        s = wd.find_element_by_id("province")     #下拉框选择
        Select(s).select_by_index(sheng)
        shi = wd.find_element_by_id("city")
        Select(shi).select_by_index(ss)
        q = wd.find_element_by_id("county")
        Select(q).select_by_index(qu)

        wd.find_element_by_name("addr").click()
        wd.find_element_by_name("addr").send_keys(addr)
        wd.find_element_by_name("phone").click()
        wd.find_element_by_name("phone").send_keys(phone)
        wd.find_element_by_name("mobile").click()
        wd.find_element_by_name("mobile").send_keys(mobile)
        if point != '':
            wd.find_element_by_name("point").clear()
            wd.find_element_by_name("point").click()
            wd.find_element_by_name("point").send_keys(point)
        wd.find_element_by_xpath("//input[@value=\"提交\"]").click()
        return wd,day

    def __del__(self):
        self.wd.quit()      #每次运行完关闭浏览器
if __name__ == "__main__":
    v = vip_user()
